#!/usr/bin/python2
# -*- coding: utf-8 -*-

import sys

from eventlet import Queue
from eventlet import spawn

from ryuapp.lib.executor2 import SequentialExecutor
from ryuapp.lib.executor_merge2 import PartialExecutor
from ryuapp.lib.hedera.FatTreeSim import FatTreeSim
from ryuapp.lib.hedera.mock_update_gen import MockUpdateGenerator
from ryuapp.lib.hedera.hedera import Hedera


class Test:
    def __init__(self, _lambda=1, mode=0):
        print("lambda=%f"%_lambda)
        self.__lambda = _lambda
        # self.__lambda = 0.2
        if mode==0:
            self.__pre_update()
        elif mode==1:
            self.__pre_update_1()

        # self.__pre_update_preformance()

    def __pre_update(self):
        update_queue = Queue()
        install_queue = Queue()
        reply_queue = Queue()
        self.__topo = FatTreeSim(install_queue,reply_queue,2)
        self.__executor = SequentialExecutor(update_queue, install_queue, reply_queue)
        self.__update_generator = MockUpdateGenerator(update_queue, self.__topo, self.__lambda)

        def run(t):
            t.run()

        a=spawn(run,self.__topo)
        b=spawn(run,self.__executor)
        c=spawn(run,self.__update_generator)
        a.wait()
        b.wait()
        c.wait()

    def __pre_update_1(self):
        common_queue = Queue()
        install_queue = Queue()
        self.__topo = FatTreeSim(install_queue,common_queue,2)
        self.__executor = PartialExecutor(common_queue, install_queue)
        self.__update_generator = MockUpdateGenerator(common_queue, self.__topo, self.__lambda)

        def run(t):
            t.run()

        a=spawn(run,self.__topo)
        b=spawn(run,self.__executor)
        c=spawn(run,self.__update_generator)
        a.wait()
        b.wait()
        c.wait()

    def __pre_update_preformance(self):
        update_queue = Queue()
        install_queue = Queue()
        reply_queue = Queue()
        self.__topo = FatTreeSim(install_queue,reply_queue,2)
        self.__executor = SequentialExecutor(update_queue, install_queue, reply_queue)
        # self.__update_generator = MockUpdateGenerator(update_queue, self.__topo, self.__lambda)
        self.__hedera = Hedera(update_queue, self.__topo, self.__lambda)

        def run(t):
            t.run()

        # a=spawn(run,self.__topo)
        b=spawn(run,self.__executor)
        c=spawn(run,self.__hedera)
        # a.wait()
        b.wait()
        c.wait()

    def __pre_update_preformance_1(self):
        common_queue = Queue()
        install_queue = Queue()
        self.__topo = FatTreeSim(install_queue,common_queue,2)
        self.__executor = PartialExecutor(common_queue, install_queue)
        # self.__update_generator = MockUpdateGenerator(common_queue, self.__topo, self.__lambda)
        self.__hedera = Hedera(common_queue, self.__topo, self.__lambda)

        def run(t):
            t.run()

        # a=spawn(run,self.__topo)
        b=spawn(run,self.__executor)
        c=spawn(run,self.__hedera)
        # a.wait()
        b.wait()
        c.wait()

#######################################

if __name__ == '__main__':
    la = 1
    if len(sys.argv)>1:
        la = float(sys.argv[1])
    mode = 0
    if len(sys.argv)>2:
        mode = int(sys.argv[2])
    Test(la, mode)
